package us.lithub;

import java.util.List;

import us.lithub.data.Friend;
import us.lithub.data.WebConnector;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * The Class Util. contains shared constants and utility code
 */
public class Util {

    // Shared constants
	
	/** The Constant PENDING. */
    public static final int PENDING = 0;
	
	/** The Constant IN_PROGRESS. */
	public static final int IN_PROGRESS = 1;
	
	/** The Constant COMPLETE. */
	public static final int COMPLETE = 2;

	/** The Constant MODE. */
	public static final String MODE = "mode";
	
	/** The Constant MY_USER_ID. */
	public static final String MY_USER_ID = "myUserId";
	
	/** The Constant USERNAME. */
	public static final String USERNAME = "username";
	
	/** The Constant PASSWORD. */
	public static final String PASSWORD = "password";
	
	/** The Constant MODE_PICK_FRIEND. */
	public static final int MODE_PICK_FRIEND = 99;
	
	/** The Constant BASE_URL. */
	public static final String BASE_URL = "192.168.1.11";
	// public static final String BASE_URL = "snyamathi.dyndns.org";

    /**
     * Key for connection status in shared preferences.
     */
    public static final String CONNECTION_STATUS = "connectionStatus";
	
	/** The Constant DIRECTION. */
	public static final String DIRECTION = "direction";
	
	/** The Constant DIRECTION_RETURN. */
	public static final int DIRECTION_RETURN = 44;
	
	/** The Constant DIRECTION_LEND. */
	public static final int DIRECTION_LEND = 22;

    /**
     * Helper method to get a SharedPreferences instance.
     *
     * @param context the context
     * @return the shared preferences
     */
    public static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences("LITHUB_SHARED_PREFS", 0);
    }

    /**
     * Checks if is registered.
     *
     * @param context the context
     * @return true if you are currently registered
     */
	public static boolean isRegistered(Context context) {
		SharedPreferences prefs = getSharedPreferences(context);
		return prefs.getBoolean(CONNECTION_STATUS, false);
	}
	
	/**
	 * Gets the username by id.
	 *
	 * @param context the context
	 * @param userID the user id
	 * @return the username by id
	 */
	public static String getUsernameById(Context context, int userID) {
		SharedPreferences prefs = getSharedPreferences(context);
		String username = prefs.getString("" + userID, null);
		if (username == null) {
			refreshUsernames(context);
		}
		
		username = prefs.getString("" + userID, null);
		return username;
	}
	
	/**
	 * Refresh usernames from web
	 *
	 * @param context the context
	 */
	public static void refreshUsernames(Context context) {
		SharedPreferences prefs = getSharedPreferences(context);
		Editor e = prefs.edit();
		
		WebConnector wc = WebConnector.getInstance();
		List<Friend> friends = wc.getAllFriends(prefs.getString(USERNAME, null), prefs.getString(PASSWORD, null));
		for (Friend friend : friends) {
			e.putString("" + friend.getUserID(), friend.getUsername());
		}
		e.commit();
	}
	
	/**
	 * Gets the my user id.
	 *
	 * @param context the context
	 * @return the my user id
	 */
	public static int getMyUserID(Context context) {
		SharedPreferences prefs = getSharedPreferences(context);
		return prefs.getInt(MY_USER_ID, 0);
	}
	
	/**
	 * Gets the username of the currently logged user
	 *
	 * @param context the context
	 * @return the username
	 */
	public static String getUsername(Context context) {
		SharedPreferences prefs = getSharedPreferences(context);
		return prefs.getString(USERNAME, null);
	}
	
	/**
	 * Gets the password of the currently logged in user
	 *
	 * @param context the context
	 * @return the password
	 */
	public static String getPassword(Context context) {
		SharedPreferences prefs = getSharedPreferences(context);
		return prefs.getString(PASSWORD, null);
	}

	/**
	 * Sets the login info (used when login is successful, or to change logins)
	 *
	 * @param context the context
	 * @param userID the user id
	 * @param username the username
	 * @param password the password
	 */
	public static void setLoginInfo(Context context, int userID, String username, String password) {
		SharedPreferences prefs = getSharedPreferences(context);
		Editor e = prefs.edit();
		
		e.putInt(MY_USER_ID, userID);
		e.putString(USERNAME, username);
		e.putString(PASSWORD, password);
		e.commit();
		
	}
}
